<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\Category;
use app\model\Goods;
use app\model\Config;

class CategoryController extends Base
{
    public function list()
    {
        $ptype = input('get.ptype');
        $data = Category::getlist(['pid' => 0, 'status' => 1, 'ptype' => $ptype]);
        foreach ($data as &$vo) {
            $vo['icon'] = toimg($vo['image']);
            $vo['name'] = $vo['title'];
        }
        return $this->json(['data' => $data]);
    }

    public function all()
    {
        $ptype = input('get.ptype');
        $config = Config::getconfig('categorystyle');
        if (empty($config['categorystyle'])) {
            $config['categorystyle'] = 1;
        }
        if ($config['categorystyle'] == 1) {
            $data = Category::getlist(['pid' => 0, 'status' => 1, 'ptype' => $ptype]);
        } elseif ($config['categorystyle'] == 2) {
            $data = Category::getcatapiall(0);
        } elseif ($config['categorystyle'] == 3) {
            $data = Category::getlist(['pid' => 0, 'status' => 1, 'ptype' => $ptype]);
            foreach ($data as &$vo) {
                $vo['image'] = toimg($vo['image']);
                $vo['goodslist'] = Goods::getGoodsBycat($vo['id']);
            }
        }

        return $this->json(['data' => $data]);
    }
    public function demand()
    {
        $data = Category::getcatapiall(0);

        return $this->json(['data' => $data]);
    }
}
